home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / QuakeTools / src / libqsys / generic.c next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  3.2 KB  |  196 lines

  1. #define    LIBQSYS_CORE
  2. #include "../include/libqsys.h"
  3.  
  4. #ifndef    SWAPSHORT
  5. short SwapShort(short l) {
  6.   unsigned char b1, b2;
  7.  
  8.   b1 = l & 255;
  9.   b2 = (l >> 8) & 255;
  10.  
  11.   return (b1 << 8) + b2;
  12. }
  13. #endif
  14.  
  15. #ifndef    SWAPLONG
  16. int SwapLong(int l) {
  17.   unsigned char b1, b2, b3, b4;
  18.  
  19.   b1 = l & 255;
  20.   b2 = (l >> 8) & 255;
  21.   b3 = (l >> 16) & 255;
  22.   b4 = (l >> 24) & 255;
  23.  
  24.   return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;
  25. }
  26. #endif
  27.  
  28. #ifndef    SWAPFLOAT
  29. float SwapFloat(float l) {
  30.   union {
  31.     unsigned char b[4];
  32.     float f;
  33.   } in, out;
  34.  
  35.   in.f = l;
  36.   out.b[0] = in.b[3];
  37.   out.b[1] = in.b[2];
  38.   out.b[2] = in.b[1];
  39.   out.b[3] = in.b[0];
  40.  
  41.   return out.f;
  42. }
  43. #endif
  44.  
  45. /*
  46.  * this is an offscreen-renderer
  47.  */
  48. #ifndef    OPENDISPLAY
  49. void *OpenDisplay(int width, int height, int depth) {
  50.   void *frameBuffer;
  51.   int bbp;
  52.   
  53.   if(depth <= 8)
  54.     bpp = 1;        /* index based */
  55.   else if(depth <= 16)
  56.     bpp = 2;        /* 15/16 bit rgb */
  57.   else if(depth <= 24)
  58.     bpp = 3;        /* 24 bit rgb */
  59.   else
  60.     bpp = 4;        /* 32 bit argb */
  61.   
  62.   if(!(frameBuffer = (void *)tmalloc(width * height * bpp)))
  63.     Error("failed to allocate offscreen framebuffer\n");
  64.   return frameBuffer;
  65. }
  66. #endif
  67.  
  68. /*
  69.  * this swaps buffer if doublebuffer
  70.  */
  71. #ifndef    SWAPDISPLAY
  72. void *SwapDisplay(void *oldBuffer) {
  73.   return oldBuffer;
  74. }
  75. #endif
  76.  
  77. #ifndef    UPDATEDISPLAY
  78. void *UpdateDisplay(void *oldBuffer, int x, int y, int width, int height) {
  79.   return oldBuffer;
  80. }
  81. #endif
  82.  
  83. #ifndef    CHANGEDISPLAY
  84. void ChangeDisplay(struct rgb *Palette) {
  85. }
  86. #endif
  87.  
  88. /*
  89.  * 
  90.  */
  91. #ifndef    CLOSEDISPLAY
  92. void CloseDisplay(void *frameBuffer) {
  93.   tfree(frameBuffer);
  94. }
  95. #endif
  96.  
  97. /*
  98.  * 
  99.  */
  100. #ifndef    OPENKEYS
  101. void OpenKeys(viod) {
  102. }
  103. #endif
  104.  
  105. /*
  106.  * the caller repeats through this untill it gets -1 or ESCAPE as a terminator
  107.  * or return FALSE
  108.  */
  109. #ifndef    GETKEYS
  110. bool GetKeys(struct keyEvent *eventBuffer) {
  111.   eventBuffer->pressed = RAWKEY_ESCAPE;
  112. }
  113. #endif
  114.  
  115. /*
  116.  * 
  117.  */
  118. #ifndef    CLOSEKEYS
  119. void CloseKeys(void) {
  120. }
  121. #endif
  122.  
  123. /*
  124.  * 
  125.  */
  126. #ifndef    OPENMOUSE
  127. void OpenMouse(void) {
  128. }
  129. #endif
  130.  
  131. /*
  132.  * the caller repeats through this untill it gets -1
  133.  */
  134. #ifndef    GETMOUSE
  135. void GetMouse(struct mouseEvent *eventBuffer) {
  136.   eventBuffer->pressed = RAWMOUSE_NOTHING;
  137.   eventBuffer->mouseX = 0;
  138.   eventBuffer->mouseY = 0;
  139. }
  140. #endif
  141.  
  142. /*
  143.  * 
  144.  */
  145. #ifndef    CLOSEMOUSE
  146. void CloseMouse(void) {
  147. }
  148. #endif
  149.  
  150. /*
  151.  * this is a generic match
  152.  */
  153. #ifndef    MATCH
  154. unsigned char Match(register struct rgb *rawpix, register struct rgb *Palette) {
  155.   unsigned char palpix = 0;
  156.   short int i;
  157.   short int match = 0x7FFF;
  158.   
  159.   /*
  160.    * save conversion? has a palette only 6 valid bits?
  161.    */
  162.   short int rawpixR = rawpix->r & 0xFFFF;
  163.   short int rawpixG = rawpix->g & 0xFFFF;
  164.   short int rawpixB = rawpix->b & 0xFFFF;
  165.  
  166.   /*
  167.    * find match 
  168.    */
  169.   for (i = 0; i < 256; i++) {
  170.     short int R, G, B, thismatch;
  171.  
  172.     if ((R = (short int)Palette[i].r - rawpixR) < 0)
  173.       R = -R;
  174.     if ((G = (short int)Palette[i].g - rawpixG) < 0)
  175.       G = -G;
  176.     if ((B = (short int)Palette[i].b - rawpixB) < 0)
  177.       B = -B;
  178.     if((thismatch = R + G + B) != 0) {
  179.       if (thismatch < match) {
  180.         match = thismatch;
  181.         palpix = (unsigned char)i;
  182.       }
  183.     }
  184.     else
  185.       return (unsigned char)i;
  186.   }
  187.   return palpix;
  188. }
  189. #endif
  190.  
  191. struct DisplayDimension localDim;
  192.  
  193. void SetDisplay(struct DisplayDimension *dim) {
  194.   localDim = *dim;
  195. }
  196.